<?php if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){?>
<div class="col-md-8">
<div class="main-content" id="main-content">
	<div class="sub-content">
		<div class="top-header page-header clearfix">
			<h1>Bid Processing</h1>
		</div><br/>
		<div class="user-profile1 col-md-12">
			<form id="bidform" name="bidform"  method="post" class="form-horizontal" role="form">
			
				<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){?>
					<div class="form-group">
								<label class="control-label col-sm-3">Admin</label>
							<div class="col-sm-9">
							<div class="select_6">
								<select id="admindropdown" name="admindropdown" onchange="getUsers('users')" class="form-control">
									<option value="">Select The Admin</option>
									<? if(count($getalladmins)>0){foreach($getalladmins as $admins){
									if($admins->user_id==$this->getproductDetails->parent_user_id){?>
										<option value="<? echo $admins->user_id;?>"><? echo $admins->company_name;?></option>
									<?}else{?>
										<option value="<? echo $admins->user_id;?>"><? echo $admins->company_name;?></option>
									<?}
									?>
									<?}}?>
								</select>
								</div>
							</div>
					</div><span style="color:red" id="erroradmin"></span>
					<? }if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']!="user"){?>
					<div class="form-group">
									<label class="control-label col-sm-3">User</label>
									<div class="col-sm-9">
									<div class="select_6">
										<select id="userdropdown" name="userdropdown" class="form-control">
											<option value="">Select The User</option>
										</select>
										</div>
									</div>	
					</div><span style="color:red" id="erroruser"></span>
					<?} else if($_SESSION['user']['user_type']=="user"){?>
					<div class="form-group" id="hiddentbox">
						<label class="control-label col-sm-3">User Name</label>
						<div class="col-sm-9">
						<input type="text" name="usertextbox" id="usertextbox" class="form-control" readonly="readonly" value="<? if(isset($_SESSION['user']['username'])){ echo $_SESSION['user']['username'];} ?>">
						<input type="hidden" name="userdropdown" id="userdropdown" value="<? if(isset($_SESSION['user']['user_id'])){ echo $_SESSION['user']['user_id'].'/'.$_SESSION['user']['username'];} ?>"></div>
					</div>
					<?}?>
					<div class="form-group">
						<label class="control-label col-sm-3">Product Name</label>
						<div class="col-sm-9">
						<input type="hidden" id="pname" name="pname" value="<? echo $this->getproductDetails->genericname;?>"/>
						<input type="text" class="input_type_text form-control" readonly="readyonly" value="<? echo $this->getproductDetails->genericname;?>"/></div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Quantity</label>
						<div class="col-sm-9">
						<input type="hidden" id="quntity" name="quantity" value="<? echo $this->getproductDetails->quntity;?>"/>
						<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->quntity.' '.$this->getproductDetails->unites;?>"/></div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Actual Price</label>
							<div class="col-sm-9">
							<? if(isset($this->getproductDetails->suggested_price) && $this->getproductDetails->suggested_price!=""){?>
								<input type="hidden" id="acprice" name="acprice" value="<? echo $this->getproductDetails->suggested_price;?>" />
								<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->suggested_price;?>" />						
							<?}?>	</div>		
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Available Packs</label>
							<div class="col-sm-9">
							<? if(isset($this->getproductDetails->per) && $this->getproductDetails->per!=""){?>
								<input type="hidden" id="hidBuyproductPacks" name="hidBuyproductPacks" value="<? echo $this->getproductDetails->package;?>" />
								<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->package;?>" />						
							<?}?></div>			
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Per Pack</label>
							<div class="col-sm-9">
							<? if(isset($this->getproductDetails->per) && $this->getproductDetails->per!=""){?>
								<input type="hidden" id="hidBuyproductPer" name="hidBuyproductPer" value="<? echo $this->getproductDetails->per;?>" />
								<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->per.' '.$this->getproductDetails->unites;?>" />						
							<?}?></div>			
					</div>
					<? if(isset($this->getproductDetails->saleing) && $this->getproductDetails->saleing!="Wholesale"){?>
					<div class="form-group">
						<label class="control-label col-sm-3">Enter Packs</label>							
						<div class="col-sm-9"><input type="text" class="input_type_text form-control" id="userPacks" name="userPacks" value="" placeholder="Enter packets"/>
					</div></div>
					<div style="color:red" id="erroruserPacks"></div>
					<?}?>
					<? if(isset($this->getproductDetails->min_price) && $this->getproductDetails->min_price!=""){?>
						<input type="hidden" id="minprice" name="minprice" value="<? echo $this->getproductDetails->min_price;?>">
					<?}?>				
					<div class="form-group">
						<label class="control-label col-sm-3">Your bid price</label>	
						<div class="col-sm-9">	<input class="input_type_text form-control" type="text" id="ybprice" name="ybprice" placeholder="Your bid price" /></div>
					</div>
					<div class="form-group">
						
							<div class="col-sm-3">&nbsp;</div>
						<div class="col-sm-9">
							   <a href="javascript:void(0)" class="btn btn-danger" role="button" onclick="return bidpriceFun()" value="submit">Bid</button>
							   <a class="back_btn btn btn-danger" role="button" href="<? echo $this->Url('products')?>/sales">Back</a>
								<div id="loading" style="display:none;float:right;margin-right:30px;"><img src="<?php echo $this->basePath() ?>/images/loading_image.gif" /></div>
							</div>
						
					</div>
					<input type="hidden" id="hidBidproductType" name="hidBidproductType" value="<? if(isset($this->getproductDetails->saleing)){echo $this->getproductDetails->saleing;}?>">
					<input type="hidden" id="hidBidproduct" name="hidBidproduct" value="<? if(isset($_GET['product'])){echo $_GET['product'];}?>">
					<input type="hidden" value="<? if(isset($this->getproductDetails->user_id) && $this->getproductDetails->user_id!=""){ echo $this->getproductDetails->user_id;}?>" id="hidajx_user_id" name="hidajx_user_id">
					<input type="hidden" id="mimiprice" name="mimiprice" value="<? if(isset($this->getproductDetails->min_price)){echo $this->getproductDetails->min_price;}?>">
					<input type="hidden" id="check_product" name="check_product" value="0">
		   	</form>
		</div>
	</div>
</div></div>
<script type="text/javascript">
$(function(){
	var productid=$("#hidBidproduct").val();	
	$.ajax({
		type: "POST",
		url:   	BASE_PATH+'/products/checking-product',
		data: {productid:productid},
			success: function(data){
				if(data.output=='notsuccess'){
					$("#check_product").val('1');
				}else if(data.output=='success'){
					$("#check_product").val('0');
				}
			}
		});
});
$(function(){
var type1='user';
	getUsers(type1);
});
	function getUsers(type){ 
		var hid_user_id='';
		<?php if(isset($_SESSION['products']['admin_user_id']) && $_SESSION['products']['admin_user_id']!=""){?>
			var user_id=$('#admindropdown').val();			
		<?php }else{?>
			var user_id=$('#admindropdown').val();	
		<?php }?>
		if(type==''){
			if($("#hidajx_user_id").val()!=""){
				var hid_user_id=$("#hidajx_user_id").val();
			}
		}else{
			var hid_user_id=$("#hidajx_user_id").val();
		}
		var check='buycheck';
		if(user_id!=''){
			$.ajax
			({
			type: "GET",
				url:   	BASE_PATH+'/products/product-ajax?user_id='+ user_id +'&user_user_id='+ hid_user_id +'&check='+check,
			success: function(data)
			{
				if(data.getadminUsers==''){
				<? if(isset($_SESSION['users']['user_type']) && $_SESSION['users']['user_type']!="user") {?>
					alert('There are no users under selected admin'); 
				<? } ?>
					$("#userdropdown").html('<option value="">Select User</option>');
				}else{
					$("#userdropdown").html(data.getadminUsers );
				}
			}
			});
		}
	
	}
function bidpriceFun(){    
   var mimiprice=$('#mimiprice').val();
   var actual_price=$("#acprice").val();
   var userbidprice=$('#ybprice').val();
   var numbers = /^[0-9]+$/;
   var packs=$('#userPacks').val();  
   var admindrop=$("#admindropdown").val();
	var userdrop=$("#userdropdown").val();
	var totalPacks=$('#hidBuyproductPacks').val();
	var type_selling=$("#hidBidproductType").val();
	if(check_product==1){
		$("#loading").hide();
		alert('Product not available');return false;		
	}
	if(admindrop=="")
		{
			$('#erroradmin').html('*Select the admin');	
				return false;
		}else{
			$('#erroradmin').html('');
		}
		if(userdrop=="")
		{
			$('#erroruser').html('*Select the user');	
				return false;
		}else{
			$('#erroruser').html('');
		}
	if(type_selling!='Wholesale'){
		if( packs == ''){
			alert("Please enter packs");return false;
		}else{
			if(isNaN(packs)){
				alert("Please enter numbers only");return false;
			}else {
				if( parseInt(packs) > parseInt(totalPacks)){
					alert("Please enter minimum packs");return false;
				}
			}
		}	
	}
	if(userbidprice=="")
	{
		alert("Please enter bid price");return false;
	}else 
	{
		if(isNaN(userbidprice)){
			alert("Please enter numbers only");return false;
		}	
	}
	if(type_selling=='Partly'){
		var  totprice=parseInt(packs)*parseInt(actual_price);
		var  bidprice=parseInt(packs)*parseInt(mimiprice);
		var totBidPrice=(parseInt(totprice)+parseInt(bidprice))/2;
		if( userbidprice < totBidPrice ){		
			alert("Enter price is very low cost of minprice.");return false;
		}else{
			$("#loading").show();
			$("#bidform").submit();
		}
	}else if(type_selling=='Wholesale'){
		 var avgBidPrice=(parseInt(actual_price)+parseInt(mimiprice))/2;
		if(userbidprice < avgBidPrice){
			alert("Enter price is very low cost of minprice.");return false;
		}else{
			$("#loading").show();
			$("#bidform").submit();
		}
	}	
}
function fillBridPrice(){
	var packs=$('#userPacks').val();
	var acPrice=$('#acprice').val();
	var totalPacks=$('#hidBuyproductPacks').val();
	var  bidPrice=parseInt(packs)*(parseInt(acPrice)/parseInt(totalPacks));
	$('#ybprice').val(bidPrice);
}
function bidPrice(){
	var bid_id=$('#boproduct').val();
	var bid_product=$('#hidBidproduct').val();
	$.ajax({
		type:	'POST',
		url:   	BASE_PATH+'/products/get-prices-on-product',
		data:	{bid_id:bid_id,bid_product:bid_product},
		success: function(data){
			if(data.output=='sucusses'){
				$("#bid_price").html(data.bid_price);
			}
		}
	});
}
$(function(){
	<? if(isset($_GET['success']) && $_GET['success']=='success'){?>
	 $("#loading").hide();
	alert("Your request has been successfully. \n Wait for approval.");
	 window.location=BASE_URL+"/products/sales";
	<?}?>
	
});
</script>
<?php }else{?>
<script>
	window.location='<? echo $this->Url('home')?>';
</script>
<?php }?>








